package com.yygo.service.impl;

import com.yygo.Constants;
import com.yygo.dao.DealerDao;
import com.yygo.dao.FeedbackDao;
import com.yygo.dao.LoanRequestDao;
import com.yygo.model.*;
import com.yygo.model.enums.Phase;
import com.yygo.service.LoanPersonManager;
import com.yygo.service.LoanRequestManager;
import com.yygo.webapp.helper.ExtendedPaginatedList;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.cache.annotation.CacheEvict;
import org.springframework.cache.annotation.CachePut;
import org.springframework.cache.annotation.Cacheable;
import org.springframework.stereotype.Service;

import java.util.ArrayList;
import java.util.Date;
import java.util.List;
import java.util.Set;

/**
 * Created by tiansha on 2015/6/13.
 */
@Service("loanRequestManager")
public class LoanRequestManagerImpl extends GenericManagerImpl<LoanRequest, Long> implements LoanRequestManager {

    private LoanRequestDao loanRequestDao;

  @Autowired
  private DealerDao dealerDao;

    @Autowired
    public LoanRequestManagerImpl(LoanRequestDao loanRequestDao) {
        super(loanRequestDao);
        this.loanRequestDao = loanRequestDao;
    }

    @Autowired
    private LoanPersonManager loanPersonManager;

    @Autowired
    private FeedbackDao feedbackDao;

    @Override
    @CachePut(value = "loanRequests", key = "#loanRequest.id")
    public LoanRequest saveLoanRequest(LoanRequest loanRequest) {
        LoanRequest request = loanRequestDao.saveLoanRequest(loanRequest);
        LoanPerson requestPerson = request.getRequestPerson();
        requestPerson.setReqId(request.getId());
        requestPerson.setRequestDate(request.getRequestDate());
        loanPersonManager.save(requestPerson);

        LoanPerson coPerson = request.getCoPerson();
        if (coPerson != null) {
            coPerson.setReqId(request.getId());
            coPerson.setRequestDate(request.getRequestDate());
            loanPersonManager.save(coPerson);
        }

        LoanPerson dbPerson = request.getDbPerson();
        if (dbPerson != null) {
            dbPerson.setReqId(request.getId());
            dbPerson.setRequestDate(request.getRequestDate());
            loanPersonManager.save(dbPerson);
        }
        return request;
    }

    @Override
    @Cacheable(value = "loanRequests", key = "#id")
    public LoanRequest loadWholeLoanRequest(Long id) {
        return loanRequestDao.loadWholeLoanRequest(id);
    }

    @Override
    public LoanRequest getLoanRequest(Long id) {
        return this.loanRequestDao.getLoanRequest(id);
    }

    @Override
    public List<Feedback> getFeedbacks(Long reqId) {
        return this.loanRequestDao.getFeedbacks(reqId);
    }

    @Override
    public List<LoanRequest> getPolicyRequests(User user, Date date, String name, String salesman, ExtendedPaginatedList paginatedList) throws IllegalAccessException{

        List results = this.loanRequestDao.getMyBondLoanRequests(user, date, name, salesman,
                paginatedList.getFirstRecordIndex(), paginatedList.getPageSize(), paginatedList.getSortDirection(), paginatedList.getSortCriterion());

        paginatedList.setList(results);
        paginatedList.setTotalNumberOfRows(loanRequestDao.getMyBondLoanRequestsCount(user, date, name, salesman));
        return results;
    }

    @Override
    public List<LoanRequest> getMyLoanRequests(String phase, User user, ExtendedPaginatedList paginatedList) {

        List results = this.loanRequestDao.getMyLoanRequests(phase, user,
                paginatedList.getFirstRecordIndex(), paginatedList.getPageSize(), paginatedList.getSortDirection(), paginatedList.getSortCriterion());

        paginatedList.setList(results);
        paginatedList.setTotalNumberOfRows(loanRequestDao.getMyLoanRequestCount(phase, user));
        return results;
    }

    @Override
    public List<LoanRequest> searchLoanRequests(User user, String status, Date date, String number, String name, ExtendedPaginatedList paginatedList) {
        List results = this.loanRequestDao.searchLoanRequests(user, status, date, number, name,
                paginatedList.getFirstRecordIndex(), paginatedList.getPageSize(), paginatedList.getSortDirection(), paginatedList.getSortCriterion());
        paginatedList.setList(results);
        paginatedList.setTotalNumberOfRows(loanRequestDao.getLoanRequestCount(user, status, date, number, name));
        return results;
    }

    @Override
    public List<LoanRequest> getLoanedToTransferRequests(User user, Date date, String number, String name, ExtendedPaginatedList paginatedList) {
        List results = this.loanRequestDao.getLoanedToTransferRequests(user, date, number, name,
                paginatedList.getFirstRecordIndex(), paginatedList.getPageSize(), paginatedList.getSortDirection(), paginatedList.getSortCriterion());
        paginatedList.setList(results);
        paginatedList.setTotalNumberOfRows(loanRequestDao.getLoanedToTransferRequestsCount(user, date, number, name));
        return results;
    }


    @Override
    public List<LoanRequest> getFeedbackRequests(User user) {
        List<Feedback> feedbacks = feedbackDao.getFeedbacks(user);
        List<LoanRequest> loanRequests = new ArrayList<>();
        for (Feedback feedback : feedbacks) {
            loanRequests.add(feedback.getLoanRequest());
        }
        return loanRequests;
    }

    @Override
    public LoanRequest getNextForRuleProcessing() {
        return loanRequestDao.getNextForRuleProcessing();
    }

    @Override
    public Set<LoanRequest> getRequestsForSlave() {
        return loanRequestDao.getRequestsForSlave();
    }

    @Override
    public LoanRequest getLoanRequestByNumber(String number) {
        return loanRequestDao.getLoanRequestByNumber(number);
    }

    @Override
    public int getRecentRequestCountByDealer(Dealer dealer, int days) {
        return loanRequestDao.getRecentRequestCountByDealer(dealer, days);
    }

    @Override
    public int getRecentDeniedRequestCountByDealer(Dealer dealer, int days) {
        return loanRequestDao.getRecentDeniedRequestCountByDealer(dealer, days);
    }

    @Override
    @CacheEvict(value = "loanRequests", allEntries = true)
    public void clearCache() {

    }
}
